package com.cwl.common.db.codegen;

import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.generator.MetaBuilder;
import com.jfinal.plugin.activerecord.generator.TableMeta;
import com.jfinal.plugin.druid.DruidPlugin;
import org.apache.commons.lang3.StringUtils;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 代码生成工具类
 */
public class CodeGenHelpler {


    /**
     * 获取数据源
     *
     * @return
     */
    public static DataSource getDatasource(String url, String username, String password) {
//        String url = "jdbc:mysql://rm-bp19ja93nb3y617563o.mysql.rds.aliyuncs.com:3306/olp_admin_dev?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8";
//        String username = "olp_admin";
//        String password = "Adsl81786685!";
        DruidPlugin druidPlugin = new DruidPlugin(url, username, password);
        druidPlugin.start();
        return druidPlugin.getDataSource();
    }

    public static MetaBuilder createMetaBuilder(String url, String username, String password) {
        MetaBuilder metaBuilder = new MetaBuilder(getDatasource(url, username, password));
        metaBuilder.setDialect(new MysqlDialect());
        return metaBuilder;
    }


    /**
     * 排除指定的表，有些表不需要生成的
     *
     * @param list
     * @param excludeTables
     */
    public static void excludeTables(List<TableMeta> list, String excludeTables) {
        if (StringUtils.isNotBlank(excludeTables)) {
            List<TableMeta> newTableMetaList = new ArrayList<>();
            Set<String> excludeTableSet = splitToSet(excludeTables.toLowerCase(), ",");
            for (TableMeta tableMeta : list) {
                if (excludeTableSet.contains(tableMeta.name.toLowerCase())) {
                    System.out.println("exclude table : " + tableMeta.name);
                    continue;
                }
                newTableMetaList.add(tableMeta);
            }
            list.clear();
            list.addAll(newTableMetaList);
        }
    }

    /**
     * 包含指定的表，用前缀来匹配，
     * @param list
     * @param prefixs 前缀，多个用","分开
     */
    public static void includeTablesByPrefix(List<TableMeta> list, String prefixs) {
        if (StringUtils.isNotBlank(prefixs)) {
            List<TableMeta> newTableMetaList = new ArrayList<>();
            Set<String> includeTableSet = splitToSet(prefixs.toLowerCase(), ",");
            for (TableMeta tableMeta : list) {
                for (String prefix : includeTableSet){
                    if (tableMeta.name.toLowerCase().startsWith(prefix)) {
                        newTableMetaList.add(tableMeta);
                        System.out.println("include table : " + tableMeta.name);
                        continue;
                    }
                }
            }
            list.clear();
            list.addAll(newTableMetaList);
        }
    }

    public static Set<String> splitToSet(String src, String regex) {
        if (src == null) {
            return null;
        }

        String[] strings = src.split(regex);
        Set<String> set = new HashSet<>();
        for (String table : strings) {
            if (StringUtils.isBlank(table)) {
                continue;
            }
            set.add(table.trim());
        }
        return set;
    }

}


